<?php
/**
 * Description of Bookmark
 *
 * @author andy
 */
class Doctrine_Bookmark extends Doctrine_Record_Generator
{
  protected $_options = array(
    'className'     => '%CLASS%Bookmark',
    'tableName'     => false,
    'generateFiles' => false,
    'table'         => false,
    'pluginTable'   => false,
    'children'      => array(),
    'options'       => array(),
  );

  /**
   * __construct
   *
   * @param string $options
   * @return void
   */
  public function __construct($options)
  {
    $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);
  }

  public function buildRelation()
  {
    $this->buildLocalRelations();
    $this->buildForeignRelations();
  }

  /**
   * buildForeignKeys
   *
   * generates foreign keys for the plugin table based on the owner table
   *
   * the foreign keys generated by this method can be used for
   * setting the relations between the owner and the plugin classes
   *
   * @param Doctrine_Table $table     the table object that owns the plugin
   * @return array                    an array of foreign key definitions
   */
  public function buildForeignKeys(Doctrine_Table $table)
  {
    $fk = array();

    foreach (
      array('component_id' => $table, 'owner_id' => $this->_options['owner_table'])
        as $name => $t)
    {
      $n = $this->_options[$name];
      $def = $t->getDefinitionOf($n['local']);

      unset($def['autoincrement']);
      unset($def['sequence']);

      $def['primary'] = true;
      $fk[$this->_options[$name]['foreign']] = $def;
    }

    return $fk;
  }

  /**
   * buildForeignRelation
   *
   * @param string $alias Alias of the foreign relation
   * @return void
   */
  public function buildForeignRelations()
  {
    $options = array('foreign'    => $this->_options['component_id']['foreign'],
      'local'  => $this->_options['component_id']['local'],
      'type'     => Doctrine_Relation::MANY);

    $this->_options['table']->getRelationParser()->bind(
      $this->_table->getComponentName(),
      $options);

    $options = array('foreign'    => $this->_options['owner_id']['foreign'],
      'local'  => $this->_options['owner_id']['local'],
      'type'     => Doctrine_Relation::MANY);

    $this->_options['owner_table']->getRelationParser()->bind(
        $this->_table->getComponentName() . ' as ' . $this->_options['table']->getComponentName() . 'Bookmarks',
        $options);
  }

  /**
   * buildLocalRelation
   *
   * @return void
   */
  public function buildLocalRelations()
  {
    $options = array('foreign'      => $this->_options['component_id']['local'],
                     'local'    => $this->_options['component_id']['foreign'],
                     'type'       => Doctrine_Relation::ONE,
                     'owningSide' => true);

    $options['onDelete'] = 'CASCADE';
    $options['onUpdate'] = 'CASCADE';

    $this->_table->getRelationParser()->bind($this->_options['table']->getComponentName(), $options);

    $options = array('foreign'    => $this->_options['owner_id']['local'],
                     'local'      => $this->_options['owner_id']['foreign'],
                     'type'       => Doctrine_Relation::ONE,
                     'owningSide' => true);

    $options['onDelete'] = 'CASCADE';
    $options['onUpdate'] = 'CASCADE';

    $this->_table->getRelationParser()->bind($this->_options['table']->getComponentName(), $options);
  }
}
